/******************************************************************************
* CHIPSEA F60 TIMER HAL
*******************************************************************************/

#include "hal_cs_f60_timer.h"

#ifdef CS_F60

#include <stdint.h>
#include <stdbool.h>


/*******************************************************************************
@brief : 配置CPU时钟为36M
@param : NONE
@ret   : NONE
*******************************************************************************/
void Config_HCLK_36M(void)
{
	RCC_PLLConfig_TypeDef PLL_InitStruct;

	PLL_InitStruct.PLL_BP  = CPU_PLL_BP;		//0;	normal mode
	PLL_InitStruct.PLL_OE  = CPU_PLL_OE;		//0;	enable Pll output
	PLL_InitStruct.PLL_SRC = CPU_PLL_SRC;		//1;	slect HIRC as input
	PLL_InitStruct.PLL_INDV  = CPU_PLL_INDV;	//PLL_INDIV_6 ;	IN_DV = 6
	PLL_InitStruct.PLL_OUTDV = CPU_PLL_OUTDV;	//PLL_OUTDIV_4;	OU_DV = 4
	PLL_InitStruct.PLL_FBDV  = CPU_PLL_FBDV;	//PLL_FBDIV_36;	FB_DV = 36  

	RCC_PLLConfig(&PLL_InitStruct);
	RCC_PLLEnable();

	while(((CLKMU -> CLKSTATUS) & BIT(2)) == 0) {}	//wait PLL stable

	RCC_HCLKSourceConfig(RCC_CLKSource_PLL, RCC_HCLKDIV_1);
}

/********************************************************
@brief : TIMER0初始化，LIRC(10KHZ)作为时钟源
@param : tcmp : 定时器计数值
@ret   : none
@tips  : none
********************************************************/
void User_Timer0_Init()
{
	RCC_APBPeriphClOCKCmd(RCC_APBPeriph_Timer0,ENABLE);
	RCC_Timer0CLKSourceConfig(RCC_CLKSource_LIRC);	//LIRC 10KHZ

	NVIC_IRQxEnable(IRQ_TMR0);
	NVIC_IRQPriorityConfig(IRQ_TMR0, 2);	//the sequence is important

	Timer_InitTypeDef Timer0_InitStruct;

	Timer0_InitStruct.Timer_IntEn 		= TIMER0_INTEN;		//ENABLE
	Timer0_InitStruct.Timer_CountMode	= TIMER0_COUNTMODE; 	//Timer_CountMode_Peroid
	Timer0_InitStruct.Timer_Prescale	= TIMER0_PRESCALE;	//0
	Timer0_InitStruct.Timer_TCMP		= (TIMER0_TCMP * 10);	//(10 * tcmp)

	Timer_Init(TIMER0, &Timer0_InitStruct);
	Timer_Enable(TIMER0);
}

/********************************************************************************
@brief : TIMER0中断 1ms
@param : NONE
@ret   : NONE
********************************************************************************/
void TMR0_IRQHandler(void)
{
	st_hal_platform.timer_flag[0] = true;

	Timer_ClearIEState(TIMER0);

	st_power.state = I2C_START;

	///////////////////////////////////////////
/*
	if(g_bDeepSleep)
	{
		I2C0->I2C_ENABLE &= ~BIT(0); //??I2C
		I2C0->I2C_CON |=(BIT(6)) ;
		I2C0->I2C_CON &=~(BIT(6)) ;
		I2C0->I2C_ENABLE |= BIT(0); //??I2C
	}
*/
	///////////////////////////////////////////
}

void Timer0_Reset(uint32_t Tcmp)
{               
	TIMER0->TCSR |= BIT(26);	//reset and disable timer0
	TIMER0->TCMPR = (10 * Tcmp);
	TIMER0->TCSR |= BIT(30);	//enable timer0      
}

void Timer1_Reset(uint32_t Tcmp)
{
	TIMER1->TCSR |= BIT(26);	//reset and disable timer1
	TIMER1->TCMPR = (10 * Tcmp);
	TIMER1->TCSR |= BIT(30);	//enable timer1
}

void hal_timer_reset(uint8_t timer, uint32_t time)
{
	if(!timer)
	{
		Timer_ClearIEState(TIMER0);
		Timer0_Reset(time);
	}
	else
	{
		Timer_ClearIEState(TIMER1);
		Timer1_Reset(time);
	}
}

#endif /* End of CS_F60 */
